import { Route, RouteProps, useLocation } from 'react-router-dom';

export default function KeepAlive({ path, component: Component }: IKeepAliveProps) {
  const { pathname } = useLocation();
  if (!Component) {
    return null;
  }
  return (
    <Route
      render={(_props) => {
        const isMatch = pathname.startsWith(path);
        return (
          <div style={{ display: isMatch ? 'block' : 'none' }}>
            <Component {..._props} />
          </div>
        );
      }}
    />
  );
}
interface IKeepAliveProps extends RouteProps {
  path: string;
}
